package com.kai.service.user.controller;

import com.kai.service.data.PagingResult;
import com.kai.service.data.Result;
import com.kai.service.data.request.system.UpdatePasswordRequest;
import com.kai.service.data.request.user.CreateUserInfoRequest;
import com.kai.service.data.request.user.PagingUserInfoRequest;
import com.kai.service.data.request.user.RegisterUserRequest;
import com.kai.service.data.request.user.UpdateUserInfoRequest;
import com.kai.service.data.response.user.UserInfoResponse;
import com.kai.service.user.service.IUserService;
import com.kai.service.util.ResultResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/system")
@Tag(name = "系统用户")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 分页获取所有账号信息
     *
     * @param pagingUserInfoRequest
     * @return
     */
    @PostMapping("/user/info:paging")
    @Operation(summary = "分页获取所有账号信息")
    public PagingResult<UserInfoResponse> pagingUserInfo(@RequestBody PagingUserInfoRequest pagingUserInfoRequest){
        return ResultResponseUtil.pageSuccess(userService.pagingUserInfo(pagingUserInfoRequest));
    }

    /**
     * 根据id获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/user/info/{userId}")
    @Operation(summary = "根据id获取用户信息")
    public Result<UserInfoResponse> getUserInfo(@PathVariable("userId") Long userId){
        return ResultResponseUtil.success(userService.getUserInfo(userId));
    }

    /**
     * 用户注册
     *
     * @param registerUserRequest
     * @return
     */
    @PostMapping("/user/register")
    @Operation(summary = "用户注册")
    public Result<Boolean> registerUser(@Valid @RequestBody RegisterUserRequest registerUserRequest){
        return ResultResponseUtil.success(userService.registerUser(registerUserRequest));
    }

    /**
     * 新增用户信息
     *
     * @param createUserInfoRequest
     * @return
     */
    @PostMapping("/user/info")
    @Operation(summary = "新增用户信息")
    public Result<Boolean> createUserInfo(@Valid @RequestBody CreateUserInfoRequest createUserInfoRequest){
        return ResultResponseUtil.success(userService.createUserInfo(createUserInfoRequest));
    }

    /**
     * 修改用户信息
     *
     * @param userId
     * @param updateUserInfoRequest
     * @return
     */
    @PutMapping("/user/info/{userId}")
    @Operation(summary = "修改用户信息")
    public Result<Boolean> updateUserInfo(@PathVariable("userId") Long userId, @RequestBody UpdateUserInfoRequest updateUserInfoRequest){
        return ResultResponseUtil.success(userService.updateUserInfo(userId, updateUserInfoRequest));
    }

    /**
     * 修改用户密码
     *
     * @param userId
     * @param updatePasswordRequest
     * @return
     */
    @PutMapping("/user/change/password/{userId}")
    @Operation(summary = "修改用户密码")
    public Result<Boolean> changePassword(@PathVariable("userId") Long userId, @Valid @RequestBody UpdatePasswordRequest updatePasswordRequest){
        return ResultResponseUtil.success(userService.changePassword(userId, updatePasswordRequest));
    }

    /**
     * 重设密码
     *
     * @param registerUserRequest
     * @return
     */
    @PutMapping("/user/reset/password")
    @Operation(summary = "重设密码")
    public Result<Boolean> resetPassword(@Valid @RequestBody RegisterUserRequest registerUserRequest){
        return ResultResponseUtil.success(userService.resetPassword(registerUserRequest));
    }

    /**
     * 删除用户信息
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/user/info/{userId}")
    @Operation(summary = "删除用户信息")
    public Result<Boolean> deleteUserInfo(@PathVariable("userId") Long userId){
        return ResultResponseUtil.success(userService.deleteUserInfo(userId));
    }
}
